﻿using System.Collections.Generic;
using System.Net.Sockets;

namespace VirtualKeyboard.Service.TcpServices
{
    internal class SocketAsyncEventArgsPool
    {
        private readonly Stack<SocketAsyncEventArgs> handlePool;
        private readonly object lockObj = new object();

        public SocketAsyncEventArgsPool(int count)
        {
            handlePool = new Stack<SocketAsyncEventArgs>(count);
        }

        public SocketAsyncEventArgs Pop()
        {
            lock (lockObj)
            {
                if (handlePool.Count == 0)
                {
                    return null;
                }
                return handlePool.Pop(); 
            }
        }

        public void Push(SocketAsyncEventArgs e)
        {
            lock (lockObj)
            {
                handlePool.Push(e); 
            }
        }
    }
}
